package org.jeecg.common.modules.doctor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.modules.doctor.entity.DoctorPatientRelation;
import org.jeecg.common.modules.doctor.mapper.DoctorPatientRelationMapper;
import org.jeecg.common.modules.doctor.service.IDoctorPatientRelationService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Objects;

/**
 * @Description: doctor_patient_relation
 * @Author: jeecg-boot
 * @Date:   2020-08-15
 * @Version: V1.0
 */
@Service
public class DoctorPatientRelationServiceImpl extends ServiceImpl<DoctorPatientRelationMapper, DoctorPatientRelation> implements IDoctorPatientRelationService {

    @Override
    public DoctorPatientRelation exists(String doctorId, String patientId) {
        LambdaQueryWrapper<DoctorPatientRelation> query = Wrappers.lambdaQuery();
        query.eq(DoctorPatientRelation::getDoctorId,doctorId);
        query.eq(DoctorPatientRelation::getPatientId,patientId);
        return getOne(query);
    }

    @Override
    public DoctorPatientRelation initRelation(String doctorId, String patientId) {
        DoctorPatientRelation patientRelation = exists(doctorId,patientId);
        if (Objects.isNull(patientRelation)){
            DoctorPatientRelation relation = new DoctorPatientRelation();
            relation.setDoctorId(doctorId);
            relation.setPatientId(patientId);
            save(relation);
            return relation;
        }
        return patientRelation;
    }
}
